package com.cw.jeeyt.dao.impl.security;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cw.jeeyt.dao.domain.security.SecurityOrgModel;
import com.cw.jeeyt.dao.intf.security.SecurityOrgDao;
import com.cw.jeeyt.dao.mapper.security.SecurityOrgMapper;
import com.cw.jeeyt.dao.model.security.SecurityOrgDo;
import com.cw.jeeyt.dao.model.security.SecurityOrgExample;
import com.cw.jeeyt.dao.query.security.SecurityOrgQuery;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.common.utils.StringUtil;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 组织机构表 dao数据库操作接口
 *
 * @author chenw
 * @version 1.0.0
 */
@Repository
public class SecurityOrgDaoImpl extends AbstractBaseDaoImpl<SecurityOrgDo, SecurityOrgMapper> implements
    SecurityOrgDao {
    @Autowired
    private SecurityOrgMapper securityOrgMapper;

    /**
     * 删除组织机构表
     *
     * @param securityOrg
     */
    @Override
    public void delete(SecurityOrgDo securityOrg) {
        securityOrgMapper.delete(securityOrg);
    }

    /**
     * 修改组织机构表
     *
     * @param securityOrg
     */
    @Override
    public void update(SecurityOrgDo securityOrg) {
        securityOrgMapper.update(securityOrg);
    }

    /**
     * 根据主键查询组织机构表
     *
     * @param orgId 主键orgId
     * @return 组织机构表
     */
    @Override
    public SecurityOrgDo getById(Long orgId) {
        return securityOrgMapper.getById(orgId);
    }

    /**
     * 分页查询组织机构表
     *
     * @param securityOrg 查询条件
     * @param startNum 开始条数
     * @param pageSize 每页长度
     * @return 组织机构表集合
     */
    @Override
    public PageInfo<SecurityOrgDo> queryByPage(SecurityOrgDo securityOrg, Integer startNum, Integer pageSize)
    {
        PageHelper.startPage(PageUtil.calPageNum(startNum, pageSize), pageSize);
        return new PageInfo<>(securityOrgMapper.queryByPage(securityOrg));
    }

    /**
     * 查询组织机构根节点信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public SecurityOrgDo queryRootSecurityOrg() {
        return securityOrgMapper.queryRootSecurityOrg();
    }

    /**
     * 根据父组织机构id查询子组织机构信息
     *
     * @param orgParentId 父组织机构id
     * @return
     * @throws Exception
     */
    @Override
    public List<SecurityOrgDo> querySecurityOrgListByParentId(Long orgParentId) {
        return securityOrgMapper.querySecurityOrgListByParentId(orgParentId);
    }

    /**
     * 更新组织机构名称
     *
     * @param orgId
     * @param orgName
     * @throws Exception
     */
    @Override
    public void updateOrgName(Long orgId, String orgName) {
        securityOrgMapper.updateOrgName(orgId, orgName);
    }

    /**
     * 更新上级组织机构
     *
     * @param orgId
     * @param orgParentId
     * @throws Exception
     */
    @Override
    public void updateOrgParentId(Long orgId, Long orgParentId) {
        securityOrgMapper.updateOrgParentId(orgId, orgParentId);
    }

    /**
     * 查看所有机构信息
     *
     * @return
     */
    @Override
    public List<SecurityOrgDo> queryOrgList() {
        return securityOrgMapper.queryOrgList();
    }

    /**
     * 根据用户ID查询机构信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<SecurityOrgModel> getSecurityOrgListByUserId(Long userId) {
        return securityOrgMapper.getSecurityOrgListByUserId(userId);
    }

    @Override
    public List<SecurityOrgModel> getSecurityOrgTreeByOrgLevelCode(String orgLevelCode) {
        return securityOrgMapper.getSecurityOrgTreeByOrgLevelCode(orgLevelCode);
    }

    /**
     * 根据用户ID查询机构信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<SecurityOrgModel> queryOrgListByUserId(Long userId) {
        return securityOrgMapper.queryOrgListByUserId(userId);
    }
    /**
     * 分页查询
     *
     * @param securityOrg 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<SecurityOrgDo> selectPage(SecurityOrgQuery securityOrg, PageParam pageParam) {
        SecurityOrgExample example = buildExample(securityOrg);
        return PageHelper.<SecurityOrgDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }
    /**
     * 根据key查询组织机构
     *
     * @param orgId
     * @return
     */
    @Override
    public SecurityOrgDo selectByKey(Long orgId) {
        SecurityOrgDo securityOrgKeyDo = new SecurityOrgDo();
        securityOrgKeyDo.setOrgId(orgId);
        return getMapper().selectByPrimaryKey(securityOrgKeyDo);
    }
    /**
     * 根据key删除组织机构
     *
     * @param orgId
     * @return
     */
    @Override
    public int deleteByKey(Long orgId) {
        SecurityOrgDo securityOrgKeyDo = new SecurityOrgDo();
        securityOrgKeyDo.setOrgId(orgId);
        return getMapper().deleteByPrimaryKey(securityOrgKeyDo);
    }
    /**
     * 查询组织机构信息
     *
     * @param securityOrg 条件
     * @return List<SecurityOrgDo>
     */
    @Override
    public List<SecurityOrgDo> selectByExample(SecurityOrgQuery securityOrg){
        return getMapper().selectByExample(buildExample(securityOrg));
    }
    /**
     * 新增组织机构信息
     *
     * @param securityOrg 条件
     * @return int>
     */
    @Override
    public int insertBySelective(SecurityOrgDo securityOrg){
        if (securityOrg == null) {
            return -1;
        }
        securityOrg.setCreateDate(new Date());
        securityOrg.setUpdateDate(new Date());
        return getMapper().insertSelective(securityOrg);
    }
    /**
     * 修改组织机构信息
     *
     * @param securityOrg
     * @return
     */
    @Override
    public int updateBySelective(SecurityOrgDo securityOrg) {
        if (securityOrg == null) {
            return -1;
        }
        securityOrg.setUpdateDate(new Date());
        return getMapper().updateByPrimaryKeySelective(securityOrg);
    }
    /**
     * 构建组织机构Example信息
     *
     * @param securityOrg
     * @return
     */
    public SecurityOrgExample buildExample(SecurityOrgQuery securityOrg){
        SecurityOrgExample example = new SecurityOrgExample();
        SecurityOrgExample.Criteria criteria = example.createCriteria();
        if (securityOrg != null) {
            //添加查询条件
            if (null != securityOrg.getOrgId()){
                criteria.andOrgIdEqualTo(securityOrg.getOrgId());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgCode())){
                criteria.andOrgCodeEqualTo(securityOrg.getOrgCode());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgName())){
                criteria.andOrgNameEqualTo(securityOrg.getOrgName());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgType())){
                criteria.andOrgTypeEqualTo(securityOrg.getOrgType());
            }
            if (null != securityOrg.getOrgParentId()){
                criteria.andOrgParentIdEqualTo(securityOrg.getOrgParentId());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgComment())){
                criteria.andOrgCommentEqualTo(securityOrg.getOrgComment());
            }
            if (null != securityOrg.getCreateBy()){
                criteria.andCreateByEqualTo(securityOrg.getCreateBy());
            }
            if (null != securityOrg.getCreateDateBegin()){
                criteria.andCreateDateGreaterThanOrEqualTo(securityOrg.getCreateDateBegin());
            }
            if (null != securityOrg.getCreateDateEnd()){
                criteria.andCreateDateLessThanOrEqualTo(securityOrg.getCreateDateEnd());
            }
            if (null != securityOrg.getUpdateBy()){
                criteria.andUpdateByEqualTo(securityOrg.getUpdateBy());
            }
            if (null != securityOrg.getUpdateDateBegin()){
                criteria.andUpdateDateGreaterThanOrEqualTo(securityOrg.getUpdateDateBegin());
            }
            if (null != securityOrg.getUpdateDateEnd()){
                criteria.andUpdateDateLessThanOrEqualTo(securityOrg.getUpdateDateEnd());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgLevelCode())){
                criteria.andOrgLevelCodeEqualTo(securityOrg.getOrgLevelCode());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgState())){
                criteria.andOrgStateEqualTo(securityOrg.getOrgState());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgAddress())){
                criteria.andOrgAddressEqualTo(securityOrg.getOrgAddress());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgPostCode())){
                criteria.andOrgPostCodeEqualTo(securityOrg.getOrgPostCode());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgEmail())){
                criteria.andOrgEmailEqualTo(securityOrg.getOrgEmail());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgPhone())){
                criteria.andOrgPhoneEqualTo(securityOrg.getOrgPhone());
            }
            if (StringUtil.isNotEmpty(securityOrg.getOrgFax())){
                criteria.andOrgFaxEqualTo(securityOrg.getOrgFax());
            }
            if (null != securityOrg.getOrgSort()){
                criteria.andOrgSortEqualTo(securityOrg.getOrgSort());
            }
            if (StringUtil.isNotEmpty(securityOrg.getManagers())){
                criteria.andManagersEqualTo(securityOrg.getManagers());
            }
            if (StringUtil.isNotEmpty(securityOrg.getManagerIds())){
                criteria.andManagerIdsEqualTo(securityOrg.getManagerIds());
            }
            if (StringUtil.isNotEmpty(securityOrg.getLeaders())){
                criteria.andLeadersEqualTo(securityOrg.getLeaders());
            }
            if (StringUtil.isNotEmpty(securityOrg.getLeaderIds())){
                criteria.andLeaderIdsEqualTo(securityOrg.getLeaderIds());
            }
            if (StringUtil.isNotEmpty(securityOrg.getTopOrgs())){
                criteria.andTopOrgsEqualTo(securityOrg.getTopOrgs());
            }
            if (StringUtil.isNotEmpty(securityOrg.getTopOrgIds())){
                criteria.andTopOrgIdsEqualTo(securityOrg.getTopOrgIds());
            }
        }
        return example;
    }
}
